package cn.xawl.gulimall.product.service.impl;

import cn.xawl.gulimall.product.entity.CategoryBrandRelationEntity;
import cn.xawl.gulimall.product.service.CategoryBrandRelationService;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.xawl.common.utils.PageUtils;
import cn.xawl.common.utils.Query;

import cn.xawl.gulimall.product.dao.BrandDao;
import cn.xawl.gulimall.product.entity.BrandEntity;
import cn.xawl.gulimall.product.service.BrandService;
import org.springframework.util.StringUtils;


@Service("brandService")
public class BrandServiceImpl extends ServiceImpl<BrandDao, BrandEntity> implements BrandService {

    @Autowired
    private CategoryBrandRelationService categoryBrandRelationService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {

        //获取查询关键字
        String key = (String) params.get("key");
        QueryWrapper<BrandEntity> wrapper = new QueryWrapper<>();
        if (StringUtils.isEmpty(key)) {
            //判断查询关键子是否为空,不为空则进行模糊查询
            wrapper.eq("brand_id", key).or().like("name", key);
        }

        IPage<BrandEntity> page = this.page(
                new Query<BrandEntity>().getPage(params),
                wrapper
        );

        return new PageUtils(page);
    }

    @Override
    public void updateCascade(BrandEntity brand) {
        this.updateById(brand);
        categoryBrandRelationService.update(new UpdateWrapper<CategoryBrandRelationEntity>().eq("brand_id", brand.getBrandId()).set("brand_name", brand.getName()));
    }

}